﻿using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;

namespace ETask.Client
{
    /// <summary>
    /// 自定义窗口缩放控件，用于无边框窗口的右下角缩放
    /// </summary>
    public class ResizeGrip : Control
    {
        #region 依赖属性
        /// <summary>
        /// 窗口最小宽度依赖属性
        /// </summary>
        public double MinWindowWidth
        {
            get => (double)GetValue(MinWindowWidthProperty);
            set => SetValue(MinWindowWidthProperty, value);
        }

        public static readonly DependencyProperty MinWindowWidthProperty =
            DependencyProperty.Register("MinWindowWidth", typeof(double), typeof(ResizeGrip), new PropertyMetadata(200.0));

        /// <summary>
        /// 窗口最小高度依赖属性
        /// </summary>
        public double MinWindowHeight
        {
            get => (double)GetValue(MinWindowHeightProperty);
            set => SetValue(MinWindowHeightProperty, value);
        }

        public static readonly DependencyProperty MinWindowHeightProperty =
            DependencyProperty.Register("MinWindowHeight", typeof(double), typeof(ResizeGrip), new PropertyMetadata(150.0));

        /// <summary>
        /// 缩放控件填充色依赖属性
        /// </summary>
        public System.Windows.Media.Brush Fill
        {
            get => (System.Windows.Media.Brush)GetValue(FillProperty);
            set => SetValue(FillProperty, value);
        }

        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register("Fill", typeof(System.Windows.Media.Brush), typeof(ResizeGrip),
                new PropertyMetadata(System.Windows.Media.Brushes.LightGray));
        #endregion

        #region 私有字段
        private bool _isResizing;
        private Point _resizeStartPoint;
        private double _startWidth;
        private double _startHeight;
        private Window _parentWindow;
        #endregion

        #region 构造函数
        static ResizeGrip()
        {
            // 设置默认样式的键
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ResizeGrip),
                new FrameworkPropertyMetadata(typeof(ResizeGrip)));
        }

        public ResizeGrip()
        {
            // 初始化事件
            Loaded += ResizeGrip_Loaded;
            Unloaded += ResizeGrip_Unloaded;
        }
        #endregion

        #region 事件处理
        private void ResizeGrip_Loaded(object sender, RoutedEventArgs e)
        {
            // 获取父窗口
            _parentWindow = Window.GetWindow(this);

            // 绑定鼠标事件
            MouseDown += OnMouseDown;
            MouseMove += OnMouseMove;
            MouseUp += OnMouseUp;
            MouseLeave += OnMouseLeave;
        }

        private void ResizeGrip_Unloaded(object sender, RoutedEventArgs e)
        {
            // 解绑事件，防止内存泄漏
            MouseDown -= OnMouseDown;
            MouseMove -= OnMouseMove;
            MouseUp -= OnMouseUp;
            MouseLeave -= OnMouseLeave;
        }

        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && _parentWindow != null)
            {
                _isResizing = true;
                _resizeStartPoint = e.GetPosition(_parentWindow);
                _startWidth = _parentWindow.Width;
                _startHeight = _parentWindow.Height;
                CaptureMouse();
            }
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (_isResizing && e.LeftButton == MouseButtonState.Pressed && _parentWindow != null)
            {
                var currentPoint = e.GetPosition(_parentWindow);

                // 计算新尺寸并应用最小尺寸限制
                _parentWindow.Width = Math.Max(MinWindowWidth, _startWidth + (currentPoint.X - _resizeStartPoint.X));
                _parentWindow.Height = Math.Max(MinWindowHeight, _startHeight + (currentPoint.Y - _resizeStartPoint.Y));
            }
        }

        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (_isResizing)
            {
                _isResizing = false;
                ReleaseMouseCapture();
            }
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            // 鼠标离开时结束缩放
            if (_isResizing)
            {
                _isResizing = false;
                ReleaseMouseCapture();
            }
        }
        #endregion
    }
}
